<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">
<link rel="preconnect" href="https://cdn.staticfile.net" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-round.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-round.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-round.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.staticfile.net/font-awesome/6.6.0/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wangjiezhe.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.21.1","exturl":true,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"disqusjs","storage":true,"lazyload":true,"nav":{"disqusjs":{"text":"Disqus","order":1},"giscus":{"text":"GitHub","order":2},"discussbot":{"text":"Telegram","order":3}},"activeClass":"disqusjs"},"stickytabs":false,"motion":{"enable":false,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"style":"flat"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="记录 Project Euler 的 Mathematica 解法">
<meta property="og:type" content="article">
<meta property="og:title" content="欧拉计划（Project Euler）解法【1~25】">
<meta property="og:url" content="https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/index.html">
<meta property="og:site_name" content="如鱼饮水">
<meta property="og:description" content="记录 Project Euler 的 Mathematica 解法">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-06T09:50:15.000Z">
<meta property="article:modified_time" content="2020-07-29T09:50:15.000Z">
<meta property="article:author" content="西风冷香">
<meta property="article:tag" content="Mathematica">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/","path":"/posts/2020-07-06-Project-Euler-1/","title":"欧拉计划（Project Euler）解法【1~25】"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>欧拉计划（Project Euler）解法【1~25】 | 如鱼饮水</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-48537410-1"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-48537410-1","only_pageview":false,"measure_protocol_api_secret":null}</script>
  <script src="/js/third-party/analytics/google-analytics.js"></script>

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?a51d31f349bffa6e3757f7d6fca0c47f"></script>


  <script data-pjax defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{&quot;token&quot;: &quot;a64c9e77fe904641b6e6b04b0e80969c&quot;}'></script>





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="如鱼饮水" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="如鱼饮水" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">如鱼饮水</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">冷暖自知</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a></li><li class="menu-item menu-item-fcircle"><a href="/fcircle/" rel="section"><i class="fa fa-user-group fa-fw"></i>朋友圈</a></li><li class="menu-item menu-item-tools"><a href="/tools/" rel="section"><i class="fa fa-rocket fa-fw"></i>工具</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#P1-Multiples-of-3-and-5"><span class="nav-text">P1: Multiples of 3 and 5</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P2-Even-Fibonacci-numbers"><span class="nav-text">P2: Even Fibonacci numbers</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P3-Largest-prime-factor"><span class="nav-text">P3: Largest prime factor</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P4-Largest-palindrome-product"><span class="nav-text">P4: Largest palindrome product</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P5-Smallest-multiple"><span class="nav-text">P5: Smallest multiple</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P6-Sum-square-difference"><span class="nav-text">P6: Sum square difference</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P7-10001st-prime"><span class="nav-text">P7: 10001st prime</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P8-Largest-product-in-a-series"><span class="nav-text">P8: Largest product in a series</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P9-Special-Pythagorean-triplet"><span class="nav-text">P9: Special Pythagorean triplet</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P10-Summation-of-primes"><span class="nav-text">P10: Summation of primes</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P11-Largest-product-in-a-grid"><span class="nav-text">P11: Largest product in a grid</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P12-Highly-divisible-triangular-number"><span class="nav-text">P12: Highly divisible triangular number</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P13-Large-sum"><span class="nav-text">P13: Large sum</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P14-Longest-Collatz-sequence"><span class="nav-text">P14: Longest Collatz sequence</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P15-Lattice-paths"><span class="nav-text">P15: Lattice paths</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P16-Power-digit-sum"><span class="nav-text">P16: Power digit sum</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P17-Number-letter-counts"><span class="nav-text">P17: Number letter counts</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P18-Maximum-path-sum-I"><span class="nav-text">P18: Maximum path sum I</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P19-Counting-Sundays"><span class="nav-text">P19: Counting Sundays</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P20-Factorial-digit-sum"><span class="nav-text">P20: Factorial digit sum</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P21-Amicable-numbers"><span class="nav-text">P21: Amicable numbers</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P22-Names-scores"><span class="nav-text">P22: Names scores</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P23-Non-abundant-sums"><span class="nav-text">P23: Non-abundant sums</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P24-Lexicographic-permutations"><span class="nav-text">P24: Lexicographic permutations</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#P25-1000-digit-Fibonacci-number"><span class="nav-text">P25: 1000-digit Fibonacci number</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="西风冷香"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">西风冷香</p>
  <div class="site-description" itemprop="description">某不知名机构不知名数学老师</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">103</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmdqaWV6aGU=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wangjiezhe"><i class="fab fa-github fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOndhbmdqaWV6aGVAZ21haWwuY29t" title="E-Mail → mailto:wangjiezhe@gmail.com"><i class="fa fa-envelope fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly90d2l0dGVyLmNvbS93YW5namllemhl" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;wangjiezhe"><i class="fab fa-twitter fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly90Lm1lL3dhbmdqaWV6aGU=" title="Telegram → https:&#x2F;&#x2F;t.me&#x2F;wangjiezhe"><i class="fab fa-telegram fa-fw"></i></span>
      </span>
  </div>
<div id="time-now">
  <canvas id="canvas" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
</div>
<script>
(function(){

  var digit=
    [
      [
        [0,0,1,1,1,0,0],
        [0,1,1,0,1,1,0],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,0,1,1,0],
        [0,0,1,1,1,0,0]
      ],//0
      [
        [0,0,0,1,1,0,0],
        [0,1,1,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [1,1,1,1,1,1,1]
      ],//1
      [
        [0,1,1,1,1,1,0],
        [1,1,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,0,0],
        [0,0,1,1,0,0,0],
        [0,1,1,0,0,0,0],
        [1,1,0,0,0,0,0],
        [1,1,0,0,0,1,1],
        [1,1,1,1,1,1,1]
      ],//2
      [
        [1,1,1,1,1,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,0,0],
        [0,0,1,1,1,0,0],
        [0,0,0,0,1,1,0],
        [0,0,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,1,1,0]
      ],//3
      [
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,1,0],
        [0,0,1,1,1,1,0],
        [0,1,1,0,1,1,0],
        [1,1,0,0,1,1,0],
        [1,1,1,1,1,1,1],
        [0,0,0,0,1,1,0],
        [0,0,0,0,1,1,0],
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,1,1]
      ],//4
      [
        [1,1,1,1,1,1,1],
        [1,1,0,0,0,0,0],
        [1,1,0,0,0,0,0],
        [1,1,1,1,1,1,0],
        [0,0,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,1,1,0]
      ],//5
      [
        [0,0,0,0,1,1,0],
        [0,0,1,1,0,0,0],
        [0,1,1,0,0,0,0],
        [1,1,0,0,0,0,0],
        [1,1,0,1,1,1,0],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,1,1,0]
      ],//6
      [
        [1,1,1,1,1,1,1],
        [1,1,0,0,0,1,1],
        [0,0,0,0,1,1,0],
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,0,0],
        [0,0,0,1,1,0,0],
        [0,0,1,1,0,0,0],
        [0,0,1,1,0,0,0],
        [0,0,1,1,0,0,0],
        [0,0,1,1,0,0,0]
      ],//7
      [
        [0,1,1,1,1,1,0],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,1,1,0],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,1,1,0]
      ],//8
      [
        [0,1,1,1,1,1,0],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [1,1,0,0,0,1,1],
        [0,1,1,1,0,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,0,1,1],
        [0,0,0,0,1,1,0],
        [0,0,0,1,1,0,0],
        [0,1,1,0,0,0,0]
      ],//9
      [
        [0,0,0,0,0,0,0],
        [0,0,1,1,1,0,0],
        [0,0,1,1,1,0,0],
        [0,0,1,1,1,0,0],
        [0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0,1,1,1,0,0],
        [0,0,1,1,1,0,0],
        [0,0,1,1,1,0,0],
        [0,0,0,0,0,0,0]
      ]//:
    ];

  var canvas = document.getElementById('canvas');

  if(canvas.getContext){
    var cxt = canvas.getContext('2d');
    //声明canvas的宽高
    var H = 100,W = 700;
    canvas.height = H;
    canvas.width = W;
    cxt.fillStyle = '#f00';
    cxt.fillRect(10,10,50,50);
  
    //存储时间数据
    var data = [];
    //存储运动的小球
    var balls = [];
    //设置粒子半径
    var R = canvas.height/20-1;
    (function(){
      var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
      //存储时间数字，由十位小时、个位小时、冒号、十位分钟、个位分钟、冒号、十位秒钟、个位秒钟这7个数字组成
      data.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
    })();
  
    /*生成点阵数字*/
    function renderDigit(index,num){
      for(var i = 0; i < digit[num].length; i++){
        for(var j = 0; j < digit[num][i].length; j++){
          if(digit[num][i][j] == 1){
            cxt.beginPath();
            cxt.arc(14*(R+2)*index + j*2*(R+1)+(R+1),i*2*(R+1)+(R+1),R,0,2*Math.PI);
            cxt.closePath();
            cxt.fill();
          }
        }
      }
    }
  
    /*更新时钟*/
    function updateDigitTime(){
      var changeNumArray = [];
      var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
      var NewData = [];
      NewData.push(temp[1],temp[2],10,temp[3],temp[4],10,temp[5],temp[6]);
      for(var i = data.length-1; i >=0 ; i--){
        //时间发生变化
        if(NewData[i] !== data[i]){
          //将变化的数字值和在data数组中的索引存储在changeNumArray数组中
          changeNumArray.push(i+'_'+(Number(data[i])+1)%10);
        }
      }
      //增加小球
      for(var i = 0; i< changeNumArray.length; i++){
        addBalls.apply(this,changeNumArray[i].split('_'));
      }
      data = NewData.concat();
    }
  
    /*更新小球状态*/
    function updateBalls(){
      for(var i = 0; i < balls.length; i++){
        balls[i].stepY += balls[i].disY;
        balls[i].x += balls[i].stepX;
        balls[i].y += balls[i].stepY;
        if(balls[i].x > W + R || balls[i].y > H + R){
          balls.splice(i,1);
          i--;
        }
      }
    }
  
    /*增加要运动的小球*/
    function addBalls(index,num){
      var numArray = [1,2,3];
      var colorArray =  ["#3BE","#09C","#A6C","#93C","#9C0","#690","#FB3","#F80","#F44","#C00"];
      for(var i = 0; i < digit[num].length; i++){
        for(var j = 0; j < digit[num][i].length; j++){
          if(digit[num][i][j] == 1){
            var ball = {
              x:14*(R+2)*index + j*2*(R+1)+(R+1),
              y:i*2*(R+1)+(R+1),
              stepX:Math.floor(Math.random() * 4 -2),
              stepY:-2*numArray[Math.floor(Math.random()*numArray.length)],
              color:colorArray[Math.floor(Math.random()*colorArray.length)],
              disY:1
            };
            balls.push(ball);
          }
        }
      }
    }
  
    /*渲染*/
    function render(){
      //重置画布宽度，达到清空画布的效果
      canvas.height = 100;
      //渲染时钟
      for(var i = 0; i < data.length; i++){
        renderDigit(i,data[i]);
      }
      //渲染小球
      for(var i = 0; i < balls.length; i++){
        cxt.beginPath();
        cxt.arc(balls[i].x,balls[i].y,R,0,2*Math.PI);
        cxt.fillStyle = balls[i].color;
        cxt.closePath();
        cxt.fill();
      }
    }
  
    clearInterval(oTimer);
    var oTimer = setInterval(function(){
      //更新时钟
      updateDigitTime();
      //更新小球状态
      updateBalls();
      //渲染
      render();
    },50);
  }

})();
</script>

<div id="site-days"></div>
<script>
  function show_date_time() {
    window.setTimeout("show_date_time()", 1000);
    BirthDay = new Date("2014/01/10 13:29:55");
    today = new Date();
    timeold = (today.getTime() - BirthDay.getTime());
    sectimeold = timeold / 1000;
    secondsold = Math.floor(sectimeold);
    msPerDay = 24 * 60 * 60 * 1000;
    e_daysold = timeold / msPerDay;
    daysold = Math.floor(e_daysold);
    e_hrsold = (e_daysold - daysold) * 24;
    hrsold = setzero(Math.floor(e_hrsold));
    e_minsold = (e_hrsold - hrsold) * 60;
    minsold = setzero(Math.floor((e_hrsold - hrsold) * 60));
    seconds = setzero(Math.floor((e_minsold - minsold) * 60));
    document.getElementById('site-days').innerHTML = "已运行" + daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
  }

  function setzero(i) {
    if (i < 10) {
      i = "0" + i
    }
    return i;
  }

  show_date_time();
</script>

        </div>
      </div>
    </div>

    
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    相关文章
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/2020-07-05-Mathematica-funny-signs/" rel="bookmark">
        <time class="popular-posts-time">2020-07-05</time>
        <br>
      Mathematica 中特殊符号的使用规则
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="西风冷香">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="如鱼饮水">
      <meta itemprop="description" content="某不知名机构不知名数学老师">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="欧拉计划（Project Euler）解法【1~25】 | 如鱼饮水">
      <meta itemprop="description" content="记录 Project Euler 的 Mathematica 解法">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          欧拉计划（Project Euler）解法【1~25】
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-07-06 17:50:15" itemprop="dateCreated datePublished" datetime="2020-07-06T17:50:15+08:00">2020-07-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2020-07-29 17:50:15" itemprop="dateModified" datetime="2020-07-29T17:50:15+08:00">2020-07-29</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E5%AD%A6/" itemprop="url" rel="index"><span itemprop="name">数学</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E5%AD%A6/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>15k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>37 分钟</span>
    </span>
</div>

            <div class="post-description">记录 Project Euler 的 Mathematica 解法</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><div class="note info"><p>我做这个练习的主要目的是为了熟悉 Mathematica 的编程和函数，而不是练习算法，因此会充分利用 Mathematica 的内置函数和特性，尽量少造轮子．</p>
</div>
<h2 id="P1-Multiples-of-3-and-5"><a class="header-anchor" href="#P1-Multiples-of-3-and-5"></a>P1: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MQ==">Multiples of 3 and 5<i class="fa fa-external-link-alt"></i></span></h2>
<p>判断一个数是否是 3 的倍数或者是 5 的倍数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Select</span><span class="punctuation">[</span><span class="built_in">Range</span><span class="punctuation">[</span><span class="number">999</span><span class="punctuation">]</span><span class="operator">,</span> <span class="built_in">Mod</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">||</span> <span class="built_in">Mod</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">5</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
<p>后来发现，Mathematica 有内置的 <code>Divisible</code> 函数，比使用 <code>Mod</code> 函数要快 20%~30%．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Select</span><span class="punctuation">[</span><span class="built_in">Range</span><span class="punctuation">[</span><span class="number">999</span><span class="punctuation">]</span><span class="operator">,</span> <span class="built_in">Divisible</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span> <span class="operator">||</span> <span class="built_in">Divisible</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">5</span><span class="punctuation">]</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span><span class="operator">//</span><span class="built_in">Timing</span></span><br></pre></td></tr></table></figure>
<h2 id="P2-Even-Fibonacci-numbers"><a class="header-anchor" href="#P2-Even-Fibonacci-numbers"></a>P2: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09Mg==">Even Fibonacci numbers<i class="fa fa-external-link-alt"></i></span></h2>
<p>四百万以下的偶 Fibonacci 数相加．</p>
<p>第一种方法是选一个适当的上界：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Select</span><span class="punctuation">[</span><span class="built_in">Fibonacci</span> <span class="operator">/@</span> <span class="built_in">Range</span><span class="punctuation">[</span><span class="number">3</span><span class="operator">,</span> <span class="number">50</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span><span class="operator">,</span> <span class="type">#</span> <span class="operator">&lt;=</span> <span class="number">4000000</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
<p>第二种是常规的累加：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Module</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">n</span> <span class="operator">=</span> <span class="number">3</span><span class="operator">,</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> <span class="variable">t</span> <span class="operator">=</span> <span class="built_in">Fibonacci</span><span class="punctuation">[</span><span class="number">3</span><span class="punctuation">]</span><span class="punctuation">&#125;</span><span class="operator">,</span></span><br><span class="line">  <span class="built_in">While</span><span class="punctuation">[</span><span class="variable">t</span> <span class="operator">&lt;=</span> <span class="number">4*^6</span><span class="operator">,</span> <span class="variable">res</span> <span class="operator">+=</span> <span class="variable">t</span><span class="operator">;</span> <span class="variable">n</span> <span class="operator">+=</span> <span class="number">3</span><span class="operator">;</span> <span class="variable">t</span> <span class="operator">=</span> <span class="built_in">Fibonacci</span><span class="punctuation">[</span><span class="variable">n</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="variable">res</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>第三种是先找到满足条件的最大的 Fibonacci 数的指针，再相加：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="built_in">Fibonacci</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="built_in">NestWhile</span><span class="punctuation">[</span><span class="punctuation">(</span><span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span> <span class="operator">&amp;,</span> <span class="number">1</span><span class="operator">,</span> <span class="built_in">Fibonacci</span><span class="punctuation">[</span><span class="type">#</span><span class="punctuation">]</span> <span class="operator">&lt;=</span> <span class="number">4*^6</span> <span class="operator">&amp;</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>时间上，第一种 &lt; 第二种 &lt; 第三种．</p>
<div class="note info"><p>但是，如果第一种方法的上界取 100，则变为 第二种 &lt; 第一种 &lt; 第三种．</p>
</div>
<div class="note success"><p>在<span class="exturl" data-url="aHR0cDovL3d3dy5tYXRoY3JhZnQub3JnL3dpa2kvaW5kZXgucGhwP3RpdGxlPVByb2plY3RfRXVsZXItTWF0aGVtYXRpY2EjUHJvYmxlbV8y">网上<i class="fa fa-external-link-alt"></i></span>见到的一种做法，时间介于第二种和第三种之间：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Last</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">&#125;</span> <span class="operator">//.</span></span><br><span class="line">  <span class="punctuation">&#123;</span><span class="punctuation">&#123;</span><span class="type">i_</span><span class="operator">,</span> <span class="type">j_</span><span class="operator">,</span> <span class="type">k_</span><span class="operator">,</span> <span class="type">n_</span><span class="punctuation">&#125;</span> <span class="operator">/;</span> <span class="variable">j</span><span class="operator">&lt;</span><span class="number">4000000</span> <span class="operator">-&gt;</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">+</span><span class="variable">k</span><span class="operator">,</span> <span class="number">2</span><span class="variable">k</span><span class="operator">+</span><span class="variable">j</span><span class="operator">,</span> <span class="number">3</span><span class="variable">k</span><span class="operator">+</span><span class="number">2</span><span class="variable">j</span><span class="operator">,</span> <span class="variable">n</span><span class="operator">+</span><span class="variable">j</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>这种方法利用了 <code>//.</code>（重复替换）来进行迭代，<code>/;</code>（条件）表示终止条件．</p>
<p>受此启发，我重写了一个更快的算法．</p>
<p>实际上，我们可以稍微对 Fibonacci 的递推公式做一下推导：</p>
<p class='katex-block'><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable rowspacing="0.25em" columnalign="right left" columnspacing="0em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><msub><mi>F</mi><mi>n</mi></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>2</mn></mrow></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mn>2</mn><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>2</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>3</mn></mrow></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mn>3</mn><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>3</mn></mrow></msub><mo>+</mo><mn>2</mn><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>4</mn></mrow></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mn>3</mn><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>3</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>4</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>5</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>6</mn></mrow></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mo>=</mo><mn>4</mn><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>3</mn></mrow></msub><mo>+</mo><msub><mi>F</mi><mrow><mi>n</mi><mo>−</mo><mn>6</mn></mrow></msub></mrow></mstyle></mtd></mtr></mtable><annotation encoding="application/x-tex">\begin{aligned}
  F_n &amp;= F_{n-1} + F_{n-2} \\
      &amp;= 2 F_{n-2} + F_{n-3} \\
      &amp;= 3 F_{n-3} + 2 F_{n-4} \\
      &amp;= 3 F_{n-3} + F_{n-4} + F_{n-5} + F_{n-6} \\
      &amp;= 4 F_{n-3} + F_{n-6}
\end{aligned}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:7.5em;vertical-align:-3.5em;"></span><span class="mord"><span class="mtable"><span class="col-align-r"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:4em;"><span style="top:-6.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">n</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span><span style="top:-4.66em;"><span class="pstrut" style="height:3em;"></span><span class="mord"></span></span><span style="top:-3.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"></span></span><span style="top:-1.66em;"><span class="pstrut" style="height:3em;"></span><span class="mord"></span></span><span style="top:-0.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:3.5em;"><span></span></span></span></span></span><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:4em;"><span style="top:-6.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span><span style="top:-4.66em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">2</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">2</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span><span style="top:-3.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">3</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord">2</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">4</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span><span style="top:-1.66em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">3</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">4</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">5</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">6</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span><span style="top:-0.16em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">4</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">3</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.1389em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">n</span><span class="mbin mtight">−</span><span class="mord mtight">6</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:3.5em;"><span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>于是上面的做法可以改写成：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Last</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="number">0</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">&#125;</span> <span class="operator">//.</span></span><br><span class="line">  <span class="punctuation">&#123;</span><span class="punctuation">&#123;</span><span class="type">i_</span><span class="operator">,</span> <span class="type">j_</span><span class="operator">,</span> <span class="type">n_</span><span class="punctuation">&#125;</span> <span class="operator">/;</span> <span class="variable">j</span><span class="operator">&lt;</span><span class="number">4000000</span> <span class="operator">-&gt;</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span> <span class="number">4</span><span class="variable">j</span><span class="operator">+</span><span class="variable">i</span><span class="operator">,</span> <span class="variable">n</span><span class="operator">+</span><span class="variable">j</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>这个算法的速度比第二种稍快一些．</p>
<p>类似的想法，也可以用 <code>NestWhile</code> 函数实现：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NestWhile</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="type">#</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">+</span> <span class="number">3</span><span class="operator">,</span> <span class="built_in">Fibonacci</span><span class="punctuation">[</span><span class="type">#</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="type">#</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">+</span> <span class="type">#</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">2</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="punctuation">&#125;</span> <span class="operator">&amp;,</span></span><br><span class="line">  <span class="punctuation">&#123;</span><span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="type">#</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">2</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">&lt;</span> <span class="number">4000000</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Last</span></span><br></pre></td></tr></table></figure>
<p>但是这种方法比上面一种方面慢了一倍，甚至比第三种还要慢，感觉是被 <code>Part</code> 函数拖慢了速度．</p>
</div>
<h2 id="P3-Largest-prime-factor"><a class="header-anchor" href="#P3-Largest-prime-factor"></a>P3: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09Mw==">Largest prime factor<i class="fa fa-external-link-alt"></i></span></h2>
<p>找最大的素因子．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">First</span> <span class="operator">/@</span> <span class="built_in">FactorInteger</span><span class="punctuation">[</span><span class="number">600851475143</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Max</span></span><br></pre></td></tr></table></figure>
<h2 id="P4-Largest-palindrome-product"><a class="header-anchor" href="#P4-Largest-palindrome-product"></a>P4: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09NA==">Largest palindrome product<i class="fa fa-external-link-alt"></i></span></h2>
<p>两个三位数相乘得到的最大回文数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">*</span><span class="variable">j</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="number">999</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span> <span class="number">100</span><span class="operator">,</span> <span class="variable">i</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Flatten</span> <span class="operator">//</span> <span class="built_in">ReverseSort</span> <span class="operator">//</span></span><br><span class="line">  <span class="built_in">SelectFirst</span><span class="punctuation">[</span><span class="built_in">PalindromeQ</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<div class="note warning"><p>前面的列表也可以用 <code>Tuples</code> 生成，不过速度慢了近一倍：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Times</span> <span class="operator">@@@</span> <span class="built_in">Tuples</span><span class="punctuation">[</span><span class="built_in">Range</span><span class="punctuation">[</span><span class="number">100</span><span class="operator">,</span> <span class="number">999</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">2</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">ReverseSort</span> <span class="operator">//</span></span><br><span class="line">  <span class="built_in">SelectFirst</span><span class="punctuation">[</span><span class="built_in">PalindromeQ</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
</div>
<h2 id="P5-Smallest-multiple"><a class="header-anchor" href="#P5-Smallest-multiple"></a>P5: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09NQ==">Smallest multiple<i class="fa fa-external-link-alt"></i></span></h2>
<p>最小公倍数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">LCM</span> <span class="operator">@@</span> <span class="built_in">Range</span><span class="punctuation">[</span><span class="number">20</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<h2 id="P6-Sum-square-difference"><a class="header-anchor" href="#P6-Sum-square-difference"></a>P6: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09Ng==">Sum square difference<i class="fa fa-external-link-alt"></i></span></h2>
<p>平方和与和平方的差．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">100</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">^</span><span class="number">2</span> <span class="operator">-</span> <span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">^</span><span class="number">2</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">100</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<div class="note success"><p>如果先把求和公式展开，再带入数值，则会快一些：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Expand</span><span class="punctuation">[</span><span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">n</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">,</span> <span class="built_in">N</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">^</span><span class="number">2</span> <span class="operator">-</span> <span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">n</span><span class="operator">^</span><span class="number">2</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">,</span> <span class="built_in">N</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">/.</span> <span class="built_in">N</span> <span class="operator">-&gt;</span> <span class="number">100</span></span><br></pre></td></tr></table></figure>
<p>不用 <code>Expand</code> 函数会更快一些，时间能缩减到第一种算法的一半：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">n</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">,</span> <span class="built_in">N</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">^</span><span class="number">2</span> <span class="operator">-</span> <span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">n</span><span class="operator">^</span><span class="number">2</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">,</span> <span class="built_in">N</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">/.</span> <span class="built_in">N</span> <span class="operator">-&gt;</span> <span class="number">100</span></span><br></pre></td></tr></table></figure>
</div>
<h2 id="P7-10001st-prime"><a class="header-anchor" href="#P7-10001st-prime"></a>P7: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09Nw==">10001st prime<i class="fa fa-external-link-alt"></i></span></h2>
<p>第 10001 个素数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Prime</span><span class="punctuation">[</span><span class="number">10001</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<h2 id="P8-Largest-product-in-a-series"><a class="header-anchor" href="#P8-Largest-product-in-a-series"></a>P8: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09OA==">Largest product in a series<i class="fa fa-external-link-alt"></i></span></h2>
<p>连续 13 位数字乘积的最大值．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">t</span> <span class="operator">=</span> <span class="number">7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">Times</span> <span class="operator">@@@</span> <span class="built_in">ToExpression</span> <span class="operator">/@</span></span><br><span class="line">   <span class="built_in">Partition</span><span class="punctuation">[</span><span class="built_in">StringSplit</span><span class="punctuation">[</span><span class="built_in">IntegerString</span><span class="operator">@</span><span class="variable">t</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">13</span><span class="operator">,</span> <span class="number">1</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Max</span></span><br></pre></td></tr></table></figure>
<div class="note success"><p>如果先把含有 <code>0</code> 的部分删除，速度会快一倍以上：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Times</span> <span class="operator">@@@</span> <span class="built_in">ToExpression</span> <span class="operator">/@</span> <span class="built_in">Select</span><span class="punctuation">[</span><span class="operator">!</span> <span class="built_in">MemberQ</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="string">&quot;0&quot;</span><span class="operator">,</span> <span class="number">2</span><span class="punctuation">]</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">@</span></span><br><span class="line">  <span class="built_in">Partition</span><span class="punctuation">[</span><span class="built_in">StringSplit</span><span class="punctuation">[</span><span class="built_in">IntegerString</span><span class="operator">@</span><span class="variable">t</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">13</span><span class="operator">,</span> <span class="number">1</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Max</span></span><br></pre></td></tr></table></figure>
</div>
<h2 id="P9-Special-Pythagorean-triplet"><a class="header-anchor" href="#P9-Special-Pythagorean-triplet"></a>P9: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09OQ==">Special Pythagorean triplet<i class="fa fa-external-link-alt"></i></span></h2>
<p>勾股方程．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">sol</span> <span class="operator">=</span> <span class="built_in">Solve</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">a</span><span class="operator">^</span><span class="number">2</span> <span class="operator">+</span> <span class="variable">b</span><span class="operator">^</span><span class="number">2</span> <span class="operator">==</span> <span class="variable">c</span><span class="operator">^</span><span class="number">2</span><span class="operator">,</span> <span class="variable">a</span> <span class="operator">+</span> <span class="variable">b</span> <span class="operator">+</span> <span class="variable">c</span> <span class="operator">==</span> <span class="number">1000</span><span class="operator">,</span> <span class="number">0</span> <span class="operator">&lt;</span> <span class="variable">a</span> <span class="operator">&lt;</span> <span class="variable">b</span> <span class="operator">&lt;</span> <span class="variable">c</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="operator">,</span> <span class="variable">c</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="built_in">Integers</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"><span class="variable">a</span> <span class="variable">b</span> <span class="variable">c</span> <span class="operator">/.</span> <span class="variable">sol</span></span><br></pre></td></tr></table></figure>
<h2 id="P10-Summation-of-primes"><a class="header-anchor" href="#P10-Summation-of-primes"></a>P10: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTA=">Summation of primes<i class="fa fa-external-link-alt"></i></span></h2>
<p>素数求和．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="built_in">Prime</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="operator">,</span><span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span><span class="built_in">PrimePi</span><span class="punctuation">[</span><span class="number">2*^6</span><span class="punctuation">]</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<div class="note success"><p>直接用 <code>Prime</code> 函数的话，耗时 14 秒．</p>
<p>改用 <code>PrimeQ</code> 函数进行筛选的话，则只需要 0.8 秒：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Range</span><span class="punctuation">[</span><span class="number">2*^6</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Select</span><span class="punctuation">[</span><span class="built_in">PrimeQ</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
</div>
<h2 id="P11-Largest-product-in-a-grid"><a class="header-anchor" href="#P11-Largest-product-in-a-grid"></a>P11: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTE=">Largest product in a grid<i class="fa fa-external-link-alt"></i></span></h2>
<p>方阵中横、竖、斜连续四个数字乘积的最大值．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">t</span> <span class="operator">=</span> <span class="string">&quot;08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 \n</span></span><br><span class="line"><span class="string">  49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 \n</span></span><br><span class="line"><span class="string">  81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 \n</span></span><br><span class="line"><span class="string">  52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 \n</span></span><br><span class="line"><span class="string">  22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 \n</span></span><br><span class="line"><span class="string">  24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 \n</span></span><br><span class="line"><span class="string">  32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 \n</span></span><br><span class="line"><span class="string">  67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 \n</span></span><br><span class="line"><span class="string">  24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 \n</span></span><br><span class="line"><span class="string">  21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 \n</span></span><br><span class="line"><span class="string">  78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 \n</span></span><br><span class="line"><span class="string">  16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 \n</span></span><br><span class="line"><span class="string">  86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 \n</span></span><br><span class="line"><span class="string">  19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 \n</span></span><br><span class="line"><span class="string">  04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 \n</span></span><br><span class="line"><span class="string">  88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 \n</span></span><br><span class="line"><span class="string">  04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 \n</span></span><br><span class="line"><span class="string">  20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 \n</span></span><br><span class="line"><span class="string">  20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 \n</span></span><br><span class="line"><span class="string">  01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48&quot;</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="variable">te</span> <span class="operator">=</span> <span class="built_in">StringSplit</span><span class="punctuation">[</span><span class="variable">t</span><span class="operator">,</span> <span class="string">&quot;\n&quot;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">StringSplit</span> <span class="operator">//</span> <span class="built_in">Map</span><span class="punctuation">[</span><span class="built_in">ToExpression</span><span class="operator">,</span> <span class="type">#</span><span class="operator">,</span> <span class="number">2</span><span class="punctuation">]</span> <span class="operator">&amp;;</span></span><br><span class="line"><span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">2</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span><span class="number">20</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span><span class="number">17</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">~</span><span class="built_in">Join</span><span class="operator">~</span></span><br><span class="line">  <span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">1</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">2</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">3</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span><span class="number">17</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span><span class="number">20</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">~</span><span class="built_in">Join</span><span class="operator">~</span></span><br><span class="line">  <span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">1</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">2</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">2</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">3</span><span class="operator">,</span><span class="variable">j</span><span class="operator">+</span><span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span><span class="number">17</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span><span class="number">17</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">~</span><span class="built_in">Join</span><span class="operator">~</span></span><br><span class="line">  <span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">1</span><span class="operator">,</span><span class="variable">j</span><span class="operator">-</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">2</span><span class="operator">,</span><span class="variable">j</span><span class="operator">-</span><span class="number">2</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">+</span><span class="number">3</span><span class="operator">,</span><span class="variable">j</span><span class="operator">-</span><span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span><span class="number">17</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span><span class="number">4</span><span class="operator">,</span><span class="number">20</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Max</span></span><br></pre></td></tr></table></figure>
<h2 id="P12-Highly-divisible-triangular-number"><a class="header-anchor" href="#P12-Highly-divisible-triangular-number"></a>P12: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTI=">Highly divisible triangular number<i class="fa fa-external-link-alt"></i></span></h2>
<p>三角数的因数个数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Module</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">n</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">,</span> <span class="variable">t</span><span class="punctuation">&#125;</span><span class="operator">,</span></span><br><span class="line"> <span class="built_in">While</span><span class="punctuation">[</span><span class="built_in">DivisorSigma</span><span class="punctuation">[</span><span class="number">0</span><span class="operator">,</span> <span class="variable">t</span> <span class="operator">=</span> <span class="variable">n</span><span class="operator">*</span><span class="punctuation">(</span><span class="variable">n</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span><span class="operator">/</span><span class="number">2</span><span class="punctuation">]</span> <span class="operator">&lt;=</span> <span class="number">500</span><span class="operator">,</span> <span class="variable">n</span><span class="operator">++</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"> <span class="variable">t</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<div class="note success"><p>受第2题启发，也可以用 <code>NestWhile</code> 函数来写，不过速度稍微慢一点。</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NestWhile</span><span class="punctuation">[</span><span class="punctuation">(</span><span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span> <span class="operator">&amp;,</span> <span class="number">1</span><span class="operator">,</span> <span class="built_in">DivisorSigma</span><span class="punctuation">[</span><span class="number">0</span><span class="operator">,</span> <span class="type">#</span> <span class="punctuation">(</span><span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span><span class="operator">/</span><span class="number">2</span><span class="punctuation">]</span> <span class="operator">&lt;=</span> <span class="number">500</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span></span><br><span class="line">  <span class="type">#</span> <span class="punctuation">(</span><span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span><span class="operator">/</span><span class="number">2</span> <span class="operator">&amp;</span></span><br></pre></td></tr></table></figure>
</div>
<h2 id="P13-Large-sum"><a class="header-anchor" href="#P13-Large-sum"></a>P13: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTM=">Large sum<i class="fa fa-external-link-alt"></i></span></h2>
<p>一百个整数相乘．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">t</span> <span class="operator">=</span> <span class="string">&quot;37107287533902102798797998220837590246510135740250 \n  46376937677490009712648124896970078050417018260538 \n  74324986199524741059474233309513058123726617309629 \n  91942213363574161572522430563301811072406154908250 \n  23067588207539346171171980310421047513778063246676 \n  89261670696623633820136378418383684178734361726757 \n  28112879812849979408065481931592621691275889832738 \n  44274228917432520321923589422876796487670272189318 \n  47451445736001306439091167216856844588711603153276 \n  70386486105843025439939619828917593665686757934951 \n  62176457141856560629502157223196586755079324193331 \n  64906352462741904929101432445813822663347944758178 \n  92575867718337217661963751590579239728245598838407 \n  58203565325359399008402633568948830189458628227828 \n  80181199384826282014278194139940567587151170094390 \n  35398664372827112653829987240784473053190104293586 \n 86515506006295864861532075273371959191420517255829 \n  71693888707715466499115593487603532921714970056938 \n  54370070576826684624621495650076471787294438377604 \n  53282654108756828443191190634694037855217779295145 \n  36123272525000296071075082563815656710885258350721 \n  45876576172410976447339110607218265236877223636045 \n 17423706905851860660448207621209813287860733969412 \n  81142660418086830619328460811191061556940512689692 \n  51934325451728388641918047049293215058642563049483 \n  62467221648435076201727918039944693004732956340691 \n  15732444386908125794514089057706229429197107928209 \n  55037687525678773091862540744969844508330393682126 \n  18336384825330154686196124348767681297534375946515 \n  80386287592878490201521685554828717201219257766954 \n  78182833757993103614740356856449095527097864797581 \n  16726320100436897842553539920931837441497806860984 \n  48403098129077791799088218795327364475675590848030 \n  87086987551392711854517078544161852424320693150332 \n  59959406895756536782107074926966537676326235447210 \n  69793950679652694742597709739166693763042633987085 \n  41052684708299085211399427365734116182760315001271 \n  65378607361501080857009149939512557028198746004375 \n  35829035317434717326932123578154982629742552737307 \n  94953759765105305946966067683156574377167401875275 \n  88902802571733229619176668713819931811048770190271 \n  25267680276078003013678680992525463401061632866526 \n  36270218540497705585629946580636237993140746255962 \n  24074486908231174977792365466257246923322810917141 \n  91430288197103288597806669760892938638285025333403 \n  34413065578016127815921815005561868836468420090470 \n  23053081172816430487623791969842487255036638784583 \n  11487696932154902810424020138335124462181441773470 \n  63783299490636259666498587618221225225512486764533 \n  67720186971698544312419572409913959008952310058822 \n  95548255300263520781532296796249481641953868218774 \n  76085327132285723110424803456124867697064507995236 \n  37774242535411291684276865538926205024910326572967 \n  23701913275725675285653248258265463092207058596522 \n  29798860272258331913126375147341994889534765745501 \n  18495701454879288984856827726077713721403798879715 \n  38298203783031473527721580348144513491373226651381 \n  34829543829199918180278916522431027392251122869539 \n  40957953066405232632538044100059654939159879593635 \n  29746152185502371307642255121183693803580388584903 \n  41698116222072977186158236678424689157993532961922 \n  62467957194401269043877107275048102390895523597457 \n  23189706772547915061505504953922979530901129967519 \n  86188088225875314529584099251203829009407770775672 \n  11306739708304724483816533873502340845647058077308 \n  82959174767140363198008187129011875491310547126581 \n  97623331044818386269515456334926366572897563400500 \n  42846280183517070527831839425882145521227251250327 \n  55121603546981200581762165212827652751691296897789 \n  32238195734329339946437501907836945765883352399886 \n  75506164965184775180738168837861091527357929701337 \n  62177842752192623401942399639168044983993173312731 \n  32924185707147349566916674687634660915035914677504 \n  99518671430235219628894890102423325116913619626622 \n  73267460800591547471830798392868535206946944540724 \n  76841822524674417161514036427982273348055556214818 \n  97142617910342598647204516893989422179826088076852 \n  87783646182799346313767754307809363333018982642090 \n  10848802521674670883215120185883543223812876952786 \n  71329612474782464538636993009049310363619763878039 \n  62184073572399794223406235393808339651327408011116 \n  66627891981488087797941876876144230030984490851411 \n  60661826293682836764744779239180335110989069790714 \n  85786944089552990653640447425576083659976645795096 \n  66024396409905389607120198219976047599490197230297 \n  64913982680032973156037120041377903785566085089252 \n  16730939319872750275468906903707539413042652315011 \n  94809377245048795150954100921645863754710598436791 \n  78639167021187492431995700641917969777599028300699 \n  15368713711936614952811305876380278410754449733078 \n  40789923115535562561142322423255033685442488917353 \n  44889911501440648020369068063960672322193204149535 \n  41503128880339536053299340368006977710650566631954 \n  81234880673210146739058568557934581403627822703280 \n  82616570773948327592232845941706525094512325230608 \n  22918802058777319719839450180888072429661980811197 \n  77158542502016545090413245809786882778948721859617 \n  72107838435069186155435662884062257473692284509516 \n  20849603980134001723930671666823555245252804609722 \n  53503534226472524250874054075591789781264330331690&quot;</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">ToExpression</span> <span class="operator">/@</span> <span class="built_in">StringSplit</span><span class="punctuation">[</span><span class="variable">t</span><span class="operator">,</span> <span class="string">&quot;\n&quot;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span> <span class="operator">//</span> <span class="built_in">IntegerString</span> <span class="operator">//</span></span><br><span class="line">  <span class="built_in">StringTake</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">10</span><span class="punctuation">]</span> <span class="operator">&amp;</span></span><br></pre></td></tr></table></figure>
<h2 id="P14-Longest-Collatz-sequence"><a class="header-anchor" href="#P14-Longest-Collatz-sequence"></a>P14: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTQ=">Longest Collatz sequence<i class="fa fa-external-link-alt"></i></span></h2>
<p>Collatz 猜想（3n+1猜想），找到一百万以下步骤最多的数字．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Table</span><span class="punctuation">[</span><span class="built_in">NestWhileList</span><span class="punctuation">[</span><span class="built_in">If</span><span class="punctuation">[</span><span class="built_in">EvenQ</span><span class="punctuation">[</span><span class="type">#</span><span class="punctuation">]</span><span class="operator">,</span> <span class="type">#</span><span class="operator">/</span><span class="number">2</span><span class="operator">,</span> <span class="number">3</span> <span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">]</span> <span class="operator">&amp;,</span> <span class="variable">n</span><span class="operator">,</span> <span class="type">#</span> <span class="operator">!=</span> <span class="number">1</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">//</span></span><br><span class="line">    <span class="built_in">Length</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">,</span> <span class="number">1000000</span> <span class="operator">-</span> <span class="number">1</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Ordering</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="operator">-</span><span class="number">1</span><span class="punctuation">]</span> <span class="operator">&amp;</span></span><br></pre></td></tr></table></figure>
<p>未经优化，计算耗时3分半．</p>
<h2 id="P15-Lattice-paths"><a class="header-anchor" href="#P15-Lattice-paths"></a>P15: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTU=">Lattice paths<i class="fa fa-external-link-alt"></i></span></h2>
<p>格点路径．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Binomial</span><span class="punctuation">[</span><span class="number">20</span> <span class="operator">+</span> <span class="number">20</span><span class="operator">,</span> <span class="number">20</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<h2 id="P16-Power-digit-sum"><a class="header-anchor" href="#P16-Power-digit-sum"></a>P16: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTY=">Power digit sum<i class="fa fa-external-link-alt"></i></span></h2>
<p>计算各位数字和．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">DigitCount</span><span class="punctuation">[</span><span class="number">2</span><span class="operator">^</span><span class="number">1000</span><span class="punctuation">]</span><span class="operator">*</span><span class="punctuation">&#123;</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">6</span><span class="operator">,</span> <span class="number">7</span><span class="operator">,</span> <span class="number">8</span><span class="operator">,</span> <span class="number">9</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">&#125;</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
<p>直接使用 <code>IntegerDigits</code> 函数会更快一些．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Total</span><span class="operator">@</span><span class="built_in">IntegerDigits</span><span class="punctuation">[</span><span class="number">2</span><span class="operator">^</span><span class="number">1000</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<h2 id="P17-Number-letter-counts"><a class="header-anchor" href="#P17-Number-letter-counts"></a>P17: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTc=">Number letter counts<i class="fa fa-external-link-alt"></i></span></h2>
<p>数字转换为英文，计算单词总长度．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">(** 获取对应数字的英文 **)</span></span><br><span class="line"><span class="variable">NumberToEnglish</span><span class="punctuation">[</span><span class="type">n_</span> <span class="operator">/;</span> <span class="number">0</span> <span class="operator">&lt;</span> <span class="variable">n</span> <span class="operator">&lt;=</span> <span class="number">1000</span><span class="punctuation">]</span> <span class="operator">:=</span></span><br><span class="line"> <span class="built_in">Module</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">hn</span><span class="operator">,</span> <span class="variable">tn</span><span class="operator">,</span> <span class="variable">table1</span><span class="operator">,</span> <span class="variable">table2</span><span class="operator">,</span> <span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="operator">,</span> <span class="variable">c</span><span class="operator">,</span> <span class="variable">d</span><span class="operator">,</span> <span class="variable">res</span><span class="punctuation">&#125;</span><span class="operator">,</span></span><br><span class="line">  <span class="variable">table1</span> <span class="operator">=</span> <span class="punctuation">&#123;</span><span class="string">&quot;one&quot;</span><span class="operator">,</span> <span class="string">&quot;two&quot;</span><span class="operator">,</span> <span class="string">&quot;three&quot;</span><span class="operator">,</span> <span class="string">&quot;four&quot;</span><span class="operator">,</span> <span class="string">&quot;five&quot;</span><span class="operator">,</span></span><br><span class="line">    <span class="string">&quot;six&quot;</span><span class="operator">,</span> <span class="string">&quot;seven&quot;</span><span class="operator">,</span> <span class="string">&quot;eight&quot;</span><span class="operator">,</span> <span class="string">&quot;nine&quot;</span><span class="operator">,</span> <span class="string">&quot;ten&quot;</span><span class="operator">,</span></span><br><span class="line">    <span class="string">&quot;eleven&quot;</span><span class="operator">,</span> <span class="string">&quot;twelve&quot;</span><span class="operator">,</span> <span class="string">&quot;thirteen&quot;</span><span class="operator">,</span> <span class="string">&quot;fourteen&quot;</span><span class="operator">,</span> <span class="string">&quot;fifteen&quot;</span><span class="operator">,</span></span><br><span class="line">    <span class="string">&quot;sixteen&quot;</span><span class="operator">,</span> <span class="string">&quot;eventeen&quot;</span><span class="operator">,</span> <span class="string">&quot;eighteen&quot;</span><span class="operator">,</span> <span class="string">&quot;nignteen&quot;</span><span class="punctuation">&#125;</span><span class="operator">;</span></span><br><span class="line">  <span class="variable">table2</span> <span class="operator">=</span> <span class="punctuation">&#123;</span><span class="string">&quot;ten&quot;</span><span class="operator">,</span> <span class="string">&quot;twenty&quot;</span><span class="operator">,</span> <span class="string">&quot;thirty&quot;</span><span class="operator">,</span> <span class="string">&quot;forty&quot;</span><span class="operator">,</span> <span class="string">&quot;fifty&quot;</span><span class="operator">,</span></span><br><span class="line">    <span class="string">&quot;sixty&quot;</span><span class="operator">,</span> <span class="string">&quot;seventy&quot;</span><span class="operator">,</span> <span class="string">&quot;eighty&quot;</span><span class="operator">,</span> <span class="string">&quot;nighty&quot;</span><span class="punctuation">&#125;</span><span class="operator">;</span></span><br><span class="line">  <span class="comment">(** 百位 **)</span></span><br><span class="line">  <span class="punctuation">&#123;</span><span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="punctuation">&#125;</span> <span class="operator">=</span> <span class="built_in">QuotientRemainder</span><span class="punctuation">[</span><span class="variable">n</span><span class="operator">,</span> <span class="number">100</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="variable">hn</span> <span class="operator">=</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">a</span> <span class="operator">!=</span> <span class="number">0</span><span class="operator">,</span> <span class="variable">table1</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="variable">a</span> <span class="operator">&lt;&gt;</span> <span class="string">&quot;hundred&quot;</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="punctuation">]</span> <span class="operator">&lt;&gt;</span></span><br><span class="line">    <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">a</span> <span class="operator">!=</span> <span class="number">0</span> <span class="operator">&amp;&amp;</span> <span class="variable">b</span> <span class="operator">!=</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;and&quot;</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="comment">(** 十位、个位 **)</span></span><br><span class="line">  <span class="punctuation">&#123;</span><span class="variable">c</span><span class="operator">,</span> <span class="variable">d</span><span class="punctuation">&#125;</span> <span class="operator">=</span> <span class="built_in">QuotientRemainder</span><span class="punctuation">[</span><span class="variable">b</span><span class="operator">,</span> <span class="number">10</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="variable">tn</span> <span class="operator">=</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">c</span> <span class="operator">&lt;</span> <span class="number">2</span><span class="operator">,</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">b</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="operator">,</span> <span class="variable">table1</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="variable">b</span><span class="punctuation">]</span><span class="operator">,</span></span><br><span class="line">    <span class="variable">table2</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="variable">c</span> <span class="operator">&lt;&gt;</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">d</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="operator">,</span> <span class="variable">table1</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="variable">d</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="comment">(** 千位 **)</span></span><br><span class="line">  <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">n</span> <span class="operator">==</span> <span class="number">1000</span><span class="operator">,</span> <span class="string">&quot;onethousand&quot;</span><span class="operator">,</span> <span class="variable">hn</span> <span class="operator">&lt;&gt;</span> <span class="variable">tn</span><span class="punctuation">]</span><span class="punctuation">]</span>；</span><br><span class="line"></span><br><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="built_in">StringLength</span><span class="operator">@</span><span class="variable">NumberToEnglish</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">1000</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<div class="note info"><p>Mathematica 有 <code>IntegerName</code> 函数可以用，但是没有 <code>and</code> 连接词．</p>
</div>
<h2 id="P18-Maximum-path-sum-I"><a class="header-anchor" href="#P18-Maximum-path-sum-I"></a>P18: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTg=">Maximum path sum I<i class="fa fa-external-link-alt"></i></span></h2>
<p>最大路径和．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">t</span> <span class="operator">=</span> <span class="string">&quot;75\n</span></span><br><span class="line"><span class="string">95 64\n</span></span><br><span class="line"><span class="string">17 47 82\n</span></span><br><span class="line"><span class="string">18 35 87 10\n</span></span><br><span class="line"><span class="string">20 04 82 47 65\n</span></span><br><span class="line"><span class="string">19 01 23 75 03 34\n</span></span><br><span class="line"><span class="string">88 02 77 73 07 63 67\n</span></span><br><span class="line"><span class="string">99 65 04 28 06 16 70 92\n</span></span><br><span class="line"><span class="string">41 41 26 56 83 40 80 70 33\n</span></span><br><span class="line"><span class="string">41 48 72 33 47 32 37 16 94 29\n</span></span><br><span class="line"><span class="string">53 71 44 65 25 43 91 52 97 51 14\n</span></span><br><span class="line"><span class="string">70 11 33 28 77 73 17 78 39 68 17 57\n</span></span><br><span class="line"><span class="string">91 71 52 38 17 14 91 43 58 50 27 29 48\n</span></span><br><span class="line"><span class="string">63 66 04 68 89 53 67 30 73 16 69 87 40 31\n</span></span><br><span class="line"><span class="string">04 62 98 27 23 09 70 98 73 93 38 53 60 04 23&quot;</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** 数据导入数组 **)</span></span><br><span class="line"><span class="variable">te</span> <span class="operator">=</span> <span class="built_in">ImportString</span><span class="punctuation">[</span><span class="variable">t</span><span class="operator">,</span> <span class="string">&quot;Table&quot;</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** 每次更新1行，获得截至该行各元素的最长路径 **)</span></span><br><span class="line"><span class="built_in">For</span><span class="punctuation">[</span><span class="variable">i</span> <span class="operator">=</span> <span class="number">2</span><span class="operator">,</span> <span class="variable">i</span> <span class="operator">&lt;=</span> <span class="built_in">Length</span><span class="operator">@</span><span class="variable">te</span><span class="operator">,</span> <span class="variable">i</span><span class="operator">++,</span></span><br><span class="line"> <span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">+=</span> <span class="built_in">MapThread</span><span class="punctuation">[</span><span class="built_in">Max</span><span class="punctuation">]</span><span class="operator">@</span><span class="punctuation">&#123;</span><span class="built_in">Prepend</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span> <span class="operator">-</span> <span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">]</span><span class="operator">,</span> <span class="built_in">Append</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span> <span class="operator">-</span> <span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="number">0</span><span class="punctuation">]</span><span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br><span class="line"><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="operator">-</span><span class="number">1</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Max</span></span><br></pre></td></tr></table></figure>
<h2 id="P19-Counting-Sundays"><a class="header-anchor" href="#P19-Counting-Sundays"></a>P19: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MTk=">Counting Sundays<i class="fa fa-external-link-alt"></i></span></h2>
<p>计算每月1日星期日的个数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">(** 获得该月的天数 **)</span></span><br><span class="line"><span class="variable">DayInMonth</span><span class="punctuation">[</span><span class="type">year_</span><span class="operator">,</span> <span class="type">month_</span><span class="punctuation">]</span> <span class="operator">:=</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">month</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">,</span></span><br><span class="line">   <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">month</span> <span class="operator">!=</span> <span class="number">2</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="number">31</span><span class="operator">,</span> <span class="number">28</span><span class="operator">,</span> <span class="number">31</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">31</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">31</span><span class="operator">,</span> <span class="number">31</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">31</span><span class="operator">,</span> <span class="number">30</span><span class="operator">,</span> <span class="number">31</span><span class="punctuation">&#125;</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="variable">month</span><span class="operator">,</span></span><br><span class="line">    <span class="built_in">If</span><span class="punctuation">[</span><span class="built_in">Mod</span><span class="punctuation">[</span><span class="variable">year</span><span class="operator">,</span> <span class="number">4</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">&amp;&amp;</span> <span class="punctuation">(</span><span class="built_in">Mod</span><span class="punctuation">[</span><span class="variable">year</span><span class="operator">,</span> <span class="number">400</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">||</span> <span class="built_in">Mod</span><span class="punctuation">[</span><span class="variable">year</span><span class="operator">,</span> <span class="number">100</span><span class="punctuation">]</span> <span class="operator">!=</span> <span class="number">0</span><span class="punctuation">)</span><span class="operator">,</span> <span class="number">29</span><span class="operator">,</span> <span class="number">28</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span></span><br><span class="line">   <span class="variable">DayInMonth</span><span class="punctuation">[</span><span class="variable">year</span> <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> <span class="variable">month</span> <span class="operator">+</span> <span class="number">12</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** 建立星期表，已知1900年1月1日为星期一 **)</span></span><br><span class="line"><span class="variable">days</span> <span class="operator">=</span> <span class="built_in">Table</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="variable">j</span><span class="operator">,</span> <span class="variable">DayInMonth</span><span class="punctuation">[</span><span class="variable">i</span><span class="operator">,</span> <span class="variable">j</span> <span class="operator">-</span> <span class="number">1</span><span class="punctuation">]</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">1900</span><span class="operator">,</span> <span class="number">2000</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">12</span><span class="punctuation">&#125;</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"><span class="variable">days</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">1</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** 获取相当于1900年1月1日的天数来计算星期几 **)</span></span><br><span class="line"><span class="variable">days</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="built_in">All</span><span class="operator">,</span> <span class="built_in">All</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">=</span></span><br><span class="line">  <span class="variable">days</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="built_in">All</span><span class="operator">,</span> <span class="built_in">All</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Flatten</span> <span class="operator">//</span> <span class="built_in">FoldList</span><span class="punctuation">[</span><span class="built_in">Plus</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Mod</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">7</span><span class="punctuation">]</span> <span class="operator">&amp;</span> <span class="operator">//</span></span><br><span class="line">   <span class="built_in">Partition</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">12</span><span class="punctuation">]</span> <span class="operator">&amp;;</span></span><br><span class="line"><span class="variable">days</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="number">2</span> <span class="operator">;;</span> <span class="built_in">Length</span><span class="operator">@</span><span class="variable">days</span><span class="operator">,</span> <span class="built_in">All</span><span class="operator">,</span> <span class="number">3</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Flatten</span> <span class="operator">//</span></span><br><span class="line">  <span class="built_in">Select</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="type">#</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">&amp;</span><span class="punctuation">]</span> <span class="operator">&amp;</span> <span class="operator">//</span> <span class="built_in">Length</span></span><br></pre></td></tr></table></figure>
<p>开始手写了判断星期的矩阵，后来发现有内置的 <code>DayName</code> 函数可以用：</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">(** 直接使用内置 DayName 函数 **)</span></span><br><span class="line"><span class="built_in">DayName</span> <span class="operator">/@</span> <span class="built_in">Flatten</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="number">1</span><span class="punctuation">]</span><span class="operator">&amp;</span> <span class="operator">@</span> <span class="built_in">Table</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="variable">j</span><span class="operator">,</span> <span class="number">1</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="number">1901</span><span class="operator">,</span> <span class="number">2000</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">12</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span></span><br><span class="line">  <span class="built_in">Select</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="type">#</span><span class="operator">==</span><span class="built_in">Sunday</span><span class="operator">&amp;</span><span class="punctuation">]</span><span class="operator">&amp;</span> <span class="operator">//</span> <span class="built_in">Length</span></span><br></pre></td></tr></table></figure>
<h2 id="P20-Factorial-digit-sum"><a class="header-anchor" href="#P20-Factorial-digit-sum"></a>P20: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjA=">Factorial digit sum<i class="fa fa-external-link-alt"></i></span></h2>
<p>阶乘的数字和．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">IntegerDigits</span><span class="operator">@</span><span class="built_in">Factorial</span><span class="punctuation">[</span><span class="number">100</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
<h2 id="P21-Amicable-numbers"><a class="header-anchor" href="#P21-Amicable-numbers"></a>P21: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjE=">Amicable numbers<i class="fa fa-external-link-alt"></i></span></h2>
<p>相亲数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">d</span><span class="punctuation">[</span><span class="type">n_</span><span class="punctuation">]</span> <span class="operator">:=</span> <span class="built_in">DivisorSigma</span><span class="punctuation">[</span><span class="number">1</span><span class="operator">,</span> <span class="variable">n</span><span class="punctuation">]</span> <span class="operator">-</span> <span class="variable">n</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** 注意排除掉 d(1)=0, d(0) 不存在，以及 d(n)=n 的情况 **)</span></span><br><span class="line"><span class="variable">AmicableQ</span><span class="punctuation">[</span><span class="type">n_</span><span class="punctuation">]</span> <span class="operator">:=</span> <span class="built_in">If</span><span class="punctuation">[</span><span class="variable">d</span><span class="punctuation">[</span><span class="variable">n</span><span class="punctuation">]</span> <span class="operator">!=</span> <span class="number">0</span> <span class="operator">&amp;&amp;</span> <span class="variable">d</span><span class="punctuation">[</span><span class="variable">n</span><span class="punctuation">]</span> <span class="operator">!=</span> <span class="variable">n</span> <span class="operator">&amp;&amp;</span> <span class="variable">d</span><span class="punctuation">[</span><span class="variable">d</span><span class="punctuation">[</span><span class="variable">n</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="variable">n</span><span class="operator">,</span> <span class="built_in">True</span><span class="operator">,</span> <span class="built_in">False</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line"></span><br><span class="line"><span class="variable">AmicableQ</span> <span class="operator">/@</span> <span class="built_in">Range</span><span class="punctuation">[</span><span class="number">10000</span> <span class="operator">-</span> <span class="number">1</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Position</span><span class="punctuation">[</span><span class="built_in">True</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Flatten</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br></pre></td></tr></table></figure>
<h2 id="P22-Names-scores"><a class="header-anchor" href="#P22-Names-scores"></a>P22: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjI=">Names scores<i class="fa fa-external-link-alt"></i></span></h2>
<p>姓名得分．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">names</span> <span class="operator">=</span> <span class="built_in">Import</span><span class="punctuation">[</span><span class="string">&quot;attachments/p022_names.txt&quot;</span><span class="operator">,</span> <span class="string">&quot;CSV&quot;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Flatten</span> <span class="operator">//</span> <span class="built_in">Sort</span><span class="operator">;</span></span><br><span class="line"><span class="variable">score</span><span class="punctuation">[</span><span class="type">name_</span><span class="punctuation">]</span> <span class="operator">:=</span> <span class="built_in">LetterNumber</span> <span class="operator">/@</span> <span class="built_in">StringSplit</span><span class="punctuation">[</span><span class="variable">name</span><span class="operator">,</span> <span class="string">&quot;&quot;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Total</span></span><br><span class="line"><span class="built_in">Sum</span><span class="punctuation">[</span><span class="variable">score</span><span class="punctuation">[</span><span class="variable">names</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">*</span><span class="variable">i</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="built_in">Length</span><span class="operator">@</span><span class="variable">names</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Timing</span></span><br></pre></td></tr></table></figure>
<h2 id="P23-Non-abundant-sums"><a class="header-anchor" href="#P23-Non-abundant-sums"></a>P23: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjM=">Non-abundant sums<i class="fa fa-external-link-alt"></i></span></h2>
<p>不能写成两个盈数之和的数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">AbundantQ</span><span class="punctuation">[</span><span class="type">n_</span><span class="punctuation">]</span> <span class="operator">:=</span> <span class="built_in">DivisorSigma</span><span class="punctuation">[</span><span class="number">1</span><span class="operator">,</span> <span class="variable">n</span><span class="punctuation">]</span> <span class="operator">&gt;</span> <span class="variable">n</span> <span class="operator">+</span> <span class="variable">n</span><span class="operator">;</span></span><br><span class="line"><span class="built_in">With</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">te</span> <span class="operator">=</span> <span class="variable">AbundantQ</span> <span class="operator">/@</span> <span class="built_in">Range</span><span class="punctuation">[</span><span class="number">28123</span><span class="punctuation">]</span> <span class="operator">//</span> <span class="built_in">Position</span><span class="punctuation">[</span><span class="built_in">True</span><span class="punctuation">]</span><span class="punctuation">&#125;</span><span class="operator">,</span></span><br><span class="line">  <span class="number">28123</span><span class="operator">*</span><span class="number">28124</span><span class="operator">/</span><span class="number">2</span> <span class="operator">-</span></span><br><span class="line">    <span class="punctuation">(</span><span class="built_in">Table</span><span class="punctuation">[</span><span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">i</span><span class="punctuation">]</span><span class="punctuation">]</span> <span class="operator">+</span> <span class="variable">te</span><span class="punctuation">[</span><span class="punctuation">[</span><span class="variable">j</span><span class="punctuation">]</span><span class="punctuation">]</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">i</span><span class="operator">,</span> <span class="built_in">Length</span><span class="operator">@</span><span class="variable">te</span><span class="punctuation">&#125;</span><span class="operator">,</span> <span class="punctuation">&#123;</span><span class="variable">j</span><span class="operator">,</span> <span class="variable">i</span><span class="punctuation">&#125;</span><span class="punctuation">]</span> <span class="operator">//</span></span><br><span class="line">      <span class="built_in">Flatten</span> <span class="operator">//</span> <span class="built_in">Select</span><span class="punctuation">[</span><span class="type">#</span><span class="operator">,</span> <span class="type">#</span><span class="operator">=</span><span class="number">28123</span><span class="operator">&amp;</span><span class="punctuation">]</span><span class="operator">&amp;</span> <span class="operator">//</span> <span class="built_in">DeleteDuplicates</span> <span class="operator">//</span> <span class="built_in">Total</span><span class="punctuation">)</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>用时1分钟．</p>
<h2 id="P24-Lexicographic-permutations"><a class="header-anchor" href="#P24-Lexicographic-permutations"></a>P24: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjQ=">Lexicographic permutations<i class="fa fa-external-link-alt"></i></span></h2>
<p>字典序排列．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Permutations</span><span class="operator">@</span><span class="punctuation">&#123;</span><span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">6</span><span class="operator">,</span> <span class="number">7</span><span class="operator">,</span> <span class="number">8</span><span class="operator">,</span> <span class="number">9</span><span class="punctuation">&#125;</span><span class="operator">~</span><span class="built_in">Part</span><span class="operator">~</span><span class="number">1000000</span> <span class="operator">//</span> <span class="built_in">FromDigits</span></span><br></pre></td></tr></table></figure>
<h2 id="P25-1000-digit-Fibonacci-number"><a class="header-anchor" href="#P25-1000-digit-Fibonacci-number"></a>P25: <span class="exturl" data-url="aHR0cHM6Ly9wcm9qZWN0ZXVsZXIubmV0L3Byb2JsZW09MjU=">1000-digit Fibonacci number<i class="fa fa-external-link-alt"></i></span></h2>
<p>一千位的 Fibonacci 数．</p>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Module</span><span class="punctuation">[</span><span class="punctuation">&#123;</span><span class="variable">n</span><span class="operator">=</span><span class="number">1</span><span class="punctuation">&#125;</span><span class="operator">,</span></span><br><span class="line">  <span class="built_in">While</span><span class="punctuation">[</span><span class="built_in">IntegerLength</span><span class="operator">@</span><span class="built_in">Fibonacci</span><span class="operator">@</span><span class="variable">n</span> <span class="operator">&lt;</span> <span class="number">1000</span><span class="operator">,</span> <span class="variable">n</span><span class="operator">++</span><span class="punctuation">]</span><span class="operator">;</span></span><br><span class="line">  <span class="variable">n</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<figure class="highlight mathematica"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NestWhile</span><span class="punctuation">[</span><span class="punctuation">(</span><span class="type">#</span> <span class="operator">+</span> <span class="number">1</span><span class="punctuation">)</span> <span class="operator">&amp;,</span> <span class="number">1</span><span class="operator">,</span> <span class="built_in">IntegerLength</span><span class="operator">@</span><span class="built_in">Fibonacci</span><span class="operator">@</span><span class="type">#</span> <span class="operator">&lt;</span> <span class="number">1000</span> <span class="operator">&amp;</span><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<p>两者用时相同．</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>西风冷香
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/" title="欧拉计划（Project Euler）解法【1~25】">https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
          <span class="social-link">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </span>

          <img class="social-item-img" src="/images/wechat_channel.jpg">
      </div>

      <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Mathematica/" rel="tag"><i class="fa fa-tag"></i> Mathematica</a>
              <a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"><i class="fa fa-tag"></i> 算法</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/2020-07-05-Mathematica-funny-signs/" rel="prev" title="Mathematica 中特殊符号的使用规则">
                  <i class="fa fa-angle-left"></i> Mathematica 中特殊符号的使用规则
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/2020-08-02-Riichi-mahjong-resources/" rel="next" title="立直麻将的相关资料整理">
                  立直麻将的相关资料整理 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      <div class="tabs tabs-comment">
        <ul class="nav-tabs">
            <li class="tab"><a href="#comment-disqusjs">Disqus</a></li>
            <li class="tab"><a href="#comment-giscus">GitHub</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane disqusjs" id="comment-disqusjs">
              
  <div class="comments disqusjs-container">
    <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
  </div>
  
            </div>
            <div class="tab-pane giscus" id="comment-giscus">
              
  
  <div class="comments giscus-container">
  </div>
  
  
            </div>
        </div>
      </div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="beian"><span class="exturl" data-url="aHR0cHM6Ly9iZWlhbi5taWl0Lmdvdi5jbg=="><nopangu>冀ICP备2024063721号-1</nopangu> </span>
      <img src="/images/gongan.png" alt=""><span class="exturl" data-url="aHR0cHM6Ly9iZWlhbi5tcHMuZ292LmNuLyMvcXVlcnkvd2ViU2VhcmNoP2NvZGU9MTMwOTAyMDIwMDA3MTY="><nopangu>冀公网安备13090202000716号</nopangu> </span>
  </div>
  <div class="copyright">
    &copy; 2014 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">西风冷香</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">223k</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl" data-url="aHR0cHM6Ly90aGVtZS1uZXh0LmpzLm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

<div class="blog-links">
    <span class="post-meta-item moe">
      <span>
        <img src="/images/icp-moe.png" style="width:auto;height:16px;" alt="">
      </span>
      <span><span class="exturl" data-url="aHR0cHM6Ly9pY3AuZ292Lm1vZS8/a2V5d29yZD0yMDI0ODE5MQ=="><nopangu>萌ICP备20248191号</nopangu></span>
      </span>
    </span>
    <span class="post-meta-item travel-moe">
      <span><span class="exturl" data-url="aHR0cHM6Ly90cmF2ZWwubW9lL2dvLmh0bWw/dHJhdmVsPW9u"><img src="/images/travel-moe.png" style="width:auto;height:18px" title="异次元之旅-跃迁-我们一起去萌站成员的星球旅行吧！"> 异次元之旅🚀</span>
      </span>
    </span>
    <span class="post-meta-item foreverblog"><span class="exturl" data-url="aHR0cHM6Ly93d3cuZm9yZXZlcmJsb2cuY24vYmxvZy8yMDkyLmh0bWw="><img src="/images/foreverblog.png" style="width:auto;height:16px;" alt="Forever Blog"></span>
    </span>
    <span class="post-meta-item wormhole"><span class="exturl" data-url="aHR0cHM6Ly93d3cuZm9yZXZlcmJsb2cuY24vZ28uaHRtbA=="><img src="/images/wormhole.gif" style="width:auto;height:24px;" alt="" title="穿梭虫洞-随机访问十年之约友链博客"></span>
    </span>
    <span class="post-meta-item travelling"><span class="exturl" data-url="aHR0cHM6Ly93d3cudHJhdmVsbGluZ3MuY24vZ28uaHRtbA=="><img src="/images/travelling.png" alt="开往-友链接力" height="18"></span>
    </span>
  
</div>

<script>
function checkWebp(callback) {
  var img = new Image();
  img.onload = function () { callback((img.width > 0) && (img.height > 0)); };
  img.onerror = function () { callback(false); };
  img.src = '';
}
function showImage(useWebp) {
  var imgs = [].slice.call(document.querySelectorAll('img'));
  imgs.forEach(function (e) {
    if ((useWebp) && !e.classList.contains('nowebp')) {
      var src = e.getAttribute('data-src')
      if (src !== null) {
        src = src.replace(/\.jpg$/, '.webp').replace(/\.jpeg$/, '.webp').replace(/\.png$/, '.webp').replace(/\.gif$/, '.webp');
        e.setAttribute('data-src', src);
      }
      src = e.getAttribute('src')
      if (src !== null) {
        src = src.replace(/\.jpg$/, '.webp').replace(/\.jpeg$/, '.webp').replace(/\.png$/, '.webp').replace(/\.gif$/, '.webp');
        e.setAttribute('src', src);
      }
    }
  });
}
</script>
<script data-pjax async>
checkWebp(showImage);
</script>


    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdn.staticfile.net/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdn.staticfile.net/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdn.staticfile.net/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdn.staticfile.net/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdn.staticfile.net/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdn.staticfile.net/pdfobject/2.3.0/pdfobject.min.js","integrity":"sha256-JJZNsid68vnh3/zyj0lY9BN5ynxVX/12XgOa1TlaYN0="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>






  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  <script src="https://cdn.staticfile.net/firebase/10.14.1/firebase-app-compat.js" integrity="sha256-KsPwUaqMNX8FDBi2qNV4Z/We2FthlJrPIRQhOVgfXMQ=" crossorigin="anonymous"></script>
  <script src="https://cdn.staticfile.net/firebase/10.14.1/firebase-firestore-compat.js" integrity="sha256-oPdfryMK6T4WGGJbidMkl/7TIOrnkCJ8BqrOT8En2a4=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="firestore" type="application/json">{"enable":true,"collection":"articles","apiKey":"AIzaSyD0AfWHrmFoKgV1x4srPQDN7rGpGclK7J4","projectId":"visitors-of-blog"}</script>
  <script src="/js/third-party/statistics/firestore.js"></script>



  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><link rel="stylesheet" href="https://cdn.staticfile.net/KaTeX/0.16.9/katex.min.css" integrity="sha256-UF1fgpAiu3tPJN/uCqEUHNe7pnr+QR0SQDNfgglgtcM=" crossorigin="anonymous">
  <script class="next-config" data-name="katex" type="application/json">{"copy_tex_js":{"url":"https://cdn.staticfile.net/KaTeX/0.16.9/contrib/copy-tex.min.js","integrity":"sha256-Us54+rSGDSTvIhKKUs4kygE2ipA0RXpWWh0/zLqw3bs="}}</script>
  <script src="/js/third-party/math/katex.js"></script>


  <script src="https://cdn.staticfile.net/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":false,"archive":false,"delay":true,"timeout":3000,"priority":true,"url":"https://wangjiezhe.gitee.io/posts/2020-07-06-Project-Euler-1/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="giscus" type="application/json">{"enable":true,"repo":"wangjiezhe/wangjiezhe.github.io","repo_id":"MDEwOlJlcG9zaXRvcnkxNTc4OTE0Ng==","category":"Announcements","category_id":"DIC_kwDOAPDsWs4Cdwtf","mapping":"og:title","strict":1,"reactions_enabled":1,"emit_metadata":1,"theme":"preferred_color_scheme","lang":"zh-CN","crossorigin":"anonymous","input_position":"top","loading":"lazy"}</script>

<script>
document.addEventListener('page:loaded', () => {
  if (!CONFIG.page.comments) return;

  NexT.utils.loadComments('.giscus-container')
    .then(() => NexT.utils.getScript('https://giscus.app/client.js', {
      attributes: {
        async                   : true,
        crossOrigin             : 'anonymous',
        'data-repo'             : CONFIG.giscus.repo,
        'data-repo-id'          : CONFIG.giscus.repo_id,
        'data-category'         : CONFIG.giscus.category,
        'data-category-id'      : CONFIG.giscus.category_id,
        'data-mapping'          : CONFIG.giscus.mapping,
        'data-strict'           : CONFIG.giscus.strict,
        'data-reactions-enabled': CONFIG.giscus.reactions_enabled,
        'data-emit-metadata'    : CONFIG.giscus.emit_metadata,
        'data-theme'            : CONFIG.giscus.theme,
        'data-lang'             : CONFIG.giscus.lang,
        'data-input-position'   : CONFIG.giscus.input_position,
        'data-loading'          : CONFIG.giscus.loading
      },
      parentNode: document.querySelector('.giscus-container')
    }));
});
</script>
<link rel="stylesheet" href="https://cdn.staticfile.net/disqusjs/3.0.2/styles/disqusjs.css" integrity="sha256-71XarXwNr1Td27HmZI9zjY+rMzRdush6/glo6VFXp7o=" crossorigin="anonymous">

<script class="next-config" data-name="disqusjs" type="application/json">{"enable":true,"api":"https://disqusjs.wangjiezhe.com/","apikey":"ENTX2OPfWXIYJpFIZ1WVpYt4F0Ri0PLUELMOCbM8HHIX9C9BqEbkBZWmK7lqKj7F","shortname":"wangjiezhe","js":{"url":"https://cdn.staticfile.net/disqusjs/3.0.2/disqusjs.es2015.umd.min.js","integrity":"sha256-okP99ZQKVpIy7+NogAMpGlIQzJa9XKXhIJcFgdju5bU="}}</script>
<script src="/js/third-party/comments/disqusjs.js"></script>



  <script async src="/js/cursor/fireworks.min.js" integrity="sha256-NVjeK0/Qni9W+mCWpzpw6IZTieyhLZJ5Kj4hdFjqSQc=" crossorigin="anonymous"></script>



<script src="/live2dw/lib/L2Dwidget.min.js?02887b17076210cf9f6ed81f0eed64e3"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/mikoto.model.json"},"display":{"position":"left"},"mobile":{"show":false},"log":false});</script></body>
</html>
